package ibg;

class SolutionTest {

	public static void main(String args[]) {
		int[] A = {1, 2, 5, 9, 9};
		System.out.println("Solution: " + solution(A, 9));
	}
	
	public static int solution(int[] A, int X) {
        int N = A.length;
        if (N == 0) {
            return -1;
        }
        int l = 0;
        int r = N - 1;
        outerloop: while (l < r) {
            int m = (l + r) / 2;
            if (A[m] > X) {
                r = m - 1;
            } else {
            	if(A[m] == X) {
            		l = m;
            		break outerloop;
            	}
            	else l = m+1;
            }
        }
        if (A[l] == X) {
            return l;
        }
        return -1;
    }
	
	
}